package com.lagou.config;

import com.lagou.enumconfig.SqlTypeEnum;
import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration){
        this.configuration = configuration;
    }

    public void parse(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();

        String namespace = rootElement.attributeValue("namespace");

        List<Element> selectNodes = rootElement.selectNodes("//select");
        for (Element selectNode : selectNodes) {
            String id = selectNode.attributeValue("id");
            String resultType = selectNode.attributeValue("resultType");
            String paramterType = selectNode.attributeValue("paramterType");
            String sql = selectNode.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement(id,resultType,paramterType,sql, SqlTypeEnum.SELECT);
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        List<Element> insertNodes = rootElement.selectNodes("//insert");
        for (Element insertNode : insertNodes) {
            String id = insertNode.attributeValue("id");
            String resultType = insertNode.attributeValue("resultType");
            String paramterType = insertNode.attributeValue("paramterType");
            String sql = insertNode.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement(id,resultType,paramterType,sql, SqlTypeEnum.INSERT);
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        List<Element> deleteNodes = rootElement.selectNodes("//delete");
        for (Element deleteNode : deleteNodes) {
            String id = deleteNode.attributeValue("id");
            String resultType = deleteNode.attributeValue("resultType");
            String paramterType = deleteNode.attributeValue("paramterType");
            String sql = deleteNode.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement(id,resultType,paramterType,sql, SqlTypeEnum.DELETE);
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        List<Element> updateNodes = rootElement.selectNodes("//update");
        for (Element updateNode : updateNodes) {
            String id = updateNode.attributeValue("id");
            String resultType = updateNode.attributeValue("resultType");
            String paramterType = updateNode.attributeValue("paramterType");
            String sql = updateNode.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement(id,resultType,paramterType,sql, SqlTypeEnum.UPDATE);
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }
    }

}
